<?php
/**
 * 设计师推荐MVP 风格图片管理
 * Date: 2020/9/8
 * Author: sunlinxin
 */
namespace Beerus\Modules\Designer\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\tools\Log;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Doraemon\service\DesignerRecomd\RecommendNote as sRecommendNote;
use Doraemon\model\DesignerRecomd\RecommendPics as mRecommendPics;

class StylePicController extends Controller
{
    const PIC_TAGS = ['', '复古', '轻奢', '美式', '中式', '法式', '北欧', '现代', '日式', '极简'];

    public function index(Request $request,Content $content)
    {
        $pageSize = $request->input("per_page", 10);
        $page = $request->input("page", 1);
        $search = [];
        $list = sRecommendNote::getPicPageList($page, $pageSize, $search);
        $gridModel = new pGridDataModel($list['list'], $list['total'], [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();

                $grid->disableFilter();

                $grid->column("id", 'ID');
                $grid->column("pic_url","图片")->image("", 100);
                $grid->column("style_tags", '标签')->display(function ($title, Grid\Column $column) {
                    return $column->label('success');
                });
                $grid->column("addtime", '添加时间')->display(function () {
                    return date("Y-m-d H:i:s", $this->addtime);
                });
                $grid->column("edittime", '更新时间')->display(function () {
                    return date("Y-m-d H:i:s", $this->edittime);
                });
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                });
            });

        $tab = new Tab();
        $tab->add('图片列表', $gridList->render(), true);
        $tab->addLink('新增图片', url('admin/Designer/pic_list/create'));
        $content->row($tab);
        return $content
            ->header('风格图片管理')
            ->description(' ')
            ->breadcrumb(['text' => '图片列表']);
    }

    public function create(Request $request, Content $content, $id = 0)
    {
        $row = [];
        if ($id) {
            $header = '编辑图片';
            $row = mRecommendPics::getByWhere(['id'=>$id]);
        } else {
            $header = '新增图片';
        }
        $tab = new Tab();
        
        $selectIndex = 0;
        if (!empty($row)) {
            if(in_array($row['style_tags'], self::PIC_TAGS)) {
                $selectIndex =  array_search($row['style_tags'], self::PIC_TAGS);
            }
        }
        
        $row['tag_index'] = $selectIndex;
        $form = new Form($row);
        $form->hidden('id');
        $form->imgUpload('pic_id','上传图片');
        //标签只能单选
        $form->select('tag_index', '选择风格标签')->options(self::PIC_TAGS)->required(); //->placeholder('请选择风格标签')
       // $form->tags('style_tags', '标签')->select(self::PIC_TAGS);
       // $form->tags('style_tags','标签')->required();

        $form->action(url('admin/Designer/pic_list/save'));

        $tab->addLink('图片列表', url('admin/Designer/pic_list'));
        $tab->add($header, $form->render(), true);
        return $content->header('风格图片管理')
            ->breadcrumb(
                ['text' => $header])
            ->row($tab);
    }

    public function save(Request $request, Content $content)
    {
        try {
            $params = $request->all();
            $styleTag = '';
            if (isset(self::PIC_TAGS[$params['tag_index']])) {
                $styleTag = self::PIC_TAGS[$params['tag_index']];
            }
            //风格必须上传
            if (empty($styleTag)) {
                $error = new MessageBag([
                    'title' => '请先选择风格标签'
                ]);
                return back()->withInput()->with(compact('error'));
            }

            //图片必须上传
            if (empty($params['pic_id'])) {
                $error = new MessageBag([
                    'title' => '请先上传图片'
                ]);
                return back()->withInput()->with(compact('error'));
            }
            unset($params['_token']);
            unset($params['tag_index']);

            $params['style_tags'] = $styleTag;

            $result = sRecommendNote::saveStylePic($params);
            if ($result) {
                return redirect(url('/admin/Designer/pic_list'))->with(compact('success'));
            } else {
                $error = new MessageBag([
                    'title' => '操作失败'
                ]);
                return back()->withInput()->with(compact('error'));
            }
        } catch (\Exception $e) {
            $error = new MessageBag([
                'title' => '操作失败,信息:' . $e->getMessage()
            ]);
            return back()->with(compact('error'));
        }
    }

    public function delPic(Request $request, $id = 0)
    {
        try {
            $result = sRecommendNote::delStylePic($id);
            if ($result) {
                return redirect(url('/admin/Designer/pic_list'))->with(compact('success'));
            } else {
                $error = new MessageBag([
                    'title' => '删除失败'
                ]);
                return back()->withInput()->with(compact('error'));
            }
        } catch (\Exception $e) {
            Log::debug($e->getMessage());
            $error = new MessageBag([
                'title' => '删除失败,信息:' . $e->getMessage()
            ]);
            return back()->with(compact('error'));
        }
    }
}